﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DJDBDK;
using Shared.Method;

namespace Shared.DJDBDK
{
    /// <summary>
    /// 语言测试卡基础类
    /// </summary>
    public class VoiceBaseClass
    {
        public static int TotalLine { get; set; }
        private static object _lockObj = new object();

        //初始化语音卡        
        public static bool InitVoiceCard(out string msg)
        {
            msg = "";
            try
            {
                Tc08a32.DisableCard();

                //加载驱动
                int DriverOpenFlag = Tc08a32.LoadDRV();
                if (DriverOpenFlag != 0)
                {
                    msg = "加载驱动失败";
                    return false;
                }
                //线路数量
                TotalLine = Tc08a32.CheckValidCh();
                if (Tc08a32.EnableCard(TotalLine, 1024 * 8) != 0)
                {
                    //关闭语音卡驱动
                    Tc08a32.FreeDRV();

                    msg = "使能语音卡失败";
                    return false;
                }

                ////设定忙音参数
                //Tc08a32.SetBusyPara(4000);
                //设定响铃参数,响铃1S，等待4S，忙音0.35S，响铃7次
                Tc08a32.SetDialPara(1600, 3400, 350, 7);

                //初始化NewSig
                NewSig.Sig_Init(0);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogTxt("Init Card", "", ex.ToString());
                return false;
            }
        }

        //关闭驱动
        public static void ExitVoiceCard()
        {
            try
            {
                ushort i;
                for (i = 0; i < TotalLine; i++)
                {
                    if (Tc08a32.CheckChTypeNew(i) == Tc08a32.CHTYPE_TRUNK)
                    {
                        Tc08a32.HangUp(i);
                        NewSig.Sig_ResetCheck(i);
                    }
                }
                Tc08a32.DisableCard();
                Tc08a32.FreeDRV();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogTxt("ExitVoiceCard", "关闭语音卡失败", ex.ToString());
            }
        }


        //拨号并判断是否响铃
        public static bool DialNum(int nChannel, string strDial, out string msg)
        {
            if (Tc08a32.CHTYPE_TRUNK != Tc08a32.CheckChTypeNew(nChannel))
            {
                msg = "请选择一条不忙的通道";
                return false;
            }

            //摘机
            //lock (_lockObj)
            //    Tc08a32.OffHook(nChannel);
            UpChannel(nChannel);

            //Tc08a32.SendDtmfBuf(nChannel, strDial);
            //0.5秒拨打一个号码
            for (int i = 0; i < strDial.Length; i++)
            {
                System.Threading.Thread.Sleep(500);
                string code = strDial.Substring(i, 1);
                lock (_lockObj)
                    Tc08a32.SendDtmfBuf(nChannel, code);
            }

            msg = "拨打号码成功";

            //关闭拨号
            ResetChannel(nChannel);

            System.Threading.Thread.Sleep(3000);

            bool ring = IsRing(nChannel);

            //System.Threading.Thread.Sleep(600);
            ////摘机
            //lock (_lockObj)
            //    Tc08a32.OffHook(nChannel);
            UpChannel(nChannel);

            //System.Threading.Thread.Sleep(1000);

            //挂机
            ResetChannel(nChannel);

            return ring;
        }

        //判断是否响铃
        private static bool IsRing(int nChannel)
        {
            bool ring = false;

            //修改判定时间为10秒
            for (int i = 0; i < 20; i++)
            {
                System.Threading.Thread.Sleep(500);
                lock (_lockObj)
                    ring = Tc08a32.RingDetect(nChannel);
                if (ring) break;
            }

            return ring;
        }

        public static void UpChannel(int nChannel)
        {
            System.Threading.Thread.Sleep(600);
            lock (_lockObj)
                Tc08a32.OffHook(nChannel);
        }

        //挂机
        public static void ResetChannel(int nChannel)
        {
            if (Tc08a32.CheckChTypeNew(nChannel) == Tc08a32.CHTYPE_TRUNK)
            {
                System.Threading.Thread.Sleep(600);
                lock (_lockObj)
                    Tc08a32.HangUp(nChannel);
            }
        }
    }
}
